<?php
namespace App\Logic\SalesReport;

use App\Models\Product\ProductSku;
use App\Models\SalesReport\PaymentDetail as TPaymentDetail;
use Illuminate\Support\Facades\DB;
class PaymentForClassifyId
{
    public static function exePayment($date){
        ini_set('memory_limit', '200M');
        set_time_limit(0);
        try{
            $start = time();
            $start_memory = memory_get_usage();
            DB::beginTransaction();
            $classify = [];
            TPaymentDetail::query()
                ->where('report_date',$date)
                ->orderBy('id')
                ->chunk(1000, function ($pds) use(&$classify){
                    foreach ($pds as $pd){
                        if(!$pd->psku_id){
                            $classifyId = 0;
                        }else{
                            if(!array_key_exists($pd->psku_id,$classify)){
                                $classify[$pd->psku_id] = ProductSku::getValueById($pd->psku_id,'classify_id');
                            }
                            $classifyId = $classify[$pd->psku_id];
                        }
                        $pd->classify_id = $classifyId;
                        $pd->save();
                    }
                });
            DB::commit();
            $end = time();
            $end_memory = memory_get_usage();
            print_r(($end - $start).'--------'.(($end_memory - $start_memory)/1024/1024));
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }
}
